package org.andao.core.binder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 属性文件工具. <p>;
 * 
 * @author Seewo software - Marco.hu(huzhiguo@cvte.cn)
 */
public final class PropertiesBinder {

	protected static Logger logger = LoggerFactory.getLogger(PropertiesBinder.class);

	/**
	 * 根据property名获取值
	 * 
	 * @param propertiesFileName 属性文件名,ex. a.properties
	 * @param propertyName 要获取的属性值名
	 * @return
	 */
	public static String getValueByPropertyName(String propertiesFileName, String propertyName) {
		return getValueByPropertyName(new File(propertiesFileName),propertyName);
	}
	
	public static String getValueByPropertyName(File proFile,String propertyName){
		String s = "";
		Properties p = new Properties();// 加载属性文件读取类
		FileInputStream in;
		try {
			// propertiesFileName如test.properties
			in = new FileInputStream(proFile);// 以流的形式读入属性文件
			p.load(in);// 属性文件将该流加入的可被读取的属性中
			in.close();// 读完了关闭
			s = p.getProperty(propertyName);// 取得对应的属性值
		} catch (Exception e) {
			logger.error("获取属性文件" + proFile.getName() + "-->" + propertyName + "出错：{}" , e.fillInStackTrace());
		}
		return s;
	}

	public static Map<String,Object> getAllValue(File proFile){
		Properties p = new Properties();// 加载属性文件读取类
		FileInputStream in;
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		try {
			in = new FileInputStream(proFile);
			p.load(in);
			in.close();
			for (Map.Entry entry : p.entrySet()) {
				map.put((String) entry.getKey(), entry.getValue());
			}
		} catch (Exception e) {
			logger.error("获取属性文件" + proFile.getName() + "的键值出错：{}", e.fillInStackTrace());
		}
		return map;
	}
	
	/**
	 * 获取某个属性文件所有键值，并用Map返回
	 * @param propertiesFileName
	 * @return 封装键值的map
	 */	
	public static Map<String, Object> getAllValue(String propertiesFileName) {		
		return getAllValue(new File(propertiesFileName));
	}

	/**
	 * 根据<code>ResourceBundle</code>获取属性文件的对应值（用于只读）
	 * 
	 * @param propertiesFileNameWithoutPostfix  属性文件名，不要带"properties"后缀
	 * @param propertyName 获取的属性值名
	 * @return
	 */
	public static String getValueByPropertyNameOnResourceBundle(String propertiesFileNameWithoutPostfix,
			String propertyName) {
		String s = "";
		// 如属性文件是test.properties，那此时propertiesFileNameWithoutPostfix的值就是test
		ResourceBundle bundel = ResourceBundle.getBundle(propertiesFileNameWithoutPostfix);
		s = bundel.getString(propertyName);
		return s;
	}

	/**
	 * 根据属性名更改属性值
	 * @param propertiesFileName 属性文件名,ex. a.properties
	 * @param propertyName 属性名
	 * @param propertyValue 更新的属性值
	 * @return 是否操作成功
	 */
	public static boolean changeValueByPropertyName(String propertiesFileName, Map proMap) {
		boolean writeOK = true;
		Properties p = new Properties();
		FileInputStream in;
		try {
			in = new FileInputStream(propertiesFileName);
			p.load(in);//
			in.close();

			Iterator it = proMap.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry entry = (Map.Entry) it.next();
				p.setProperty(entry.getKey().toString(), entry.getValue().toString());
			}

			FileOutputStream out = new FileOutputStream(propertiesFileName);// 输出流
			p.store(out, "greenhome config");// 设置属性头，如不想设置，请把后面一个用""替换掉
			out.flush();// 清空缓存，写入磁盘
			out.close();// 关闭输出流
		} catch (Exception e) {
			writeOK = false;
			logger.error("更改属性文件" + propertiesFileName + "键值出错：" + e.fillInStackTrace());
		}
		return writeOK;
	}

	/**
	 * 获取系统环境变量
	 * @return
	 */
	public static Map getEnv() {
		return System.getenv();
	}
}
